/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.image;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.source.entity.image.EleSourceImage;
import com.thinkgem.jeesite.modules.source.entity.image.EleSourceImageGroup;
import com.thinkgem.jeesite.modules.source.service.image.EleSourceImageGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 图片资源管理Controller
 * @author ZC
 * @version 2017-04-20
 */
@Controller
@RequestMapping(value = "${adminPath}/source/image/eleSourceImageGroup")
public class EleSourceImageGroupController extends BaseController {

	@Autowired
	private EleSourceImageGroupService eleSourceImageGroupService;
	
	@ModelAttribute
	public EleSourceImageGroup get(@RequestParam(required=false) String id) {
		EleSourceImageGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceImageGroupService.get(id);
		}
		if (entity == null){
			entity = new EleSourceImageGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("source:image:eleSourceImageGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceImageGroup eleSourceImageGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleSourceImageGroup> page = eleSourceImageGroupService.findPage(new Page<EleSourceImageGroup>(request, response), eleSourceImageGroup); 
		model.addAttribute("page", page);
		return "modules/source/image/eleSourceImageGroupList";
	}

	@RequiresPermissions("source:image:eleSourceImageGroup:view")
	@RequestMapping(value = "form")
	public String form(EleSourceImageGroup eleSourceImageGroup, Model model) {
		model.addAttribute("eleSourceImageGroup", eleSourceImageGroup);
		return "modules/source/image/eleSourceImageGroupForm";
	}

	@RequiresPermissions("source:image:eleSourceImageGroup:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceImageGroup eleSourceImageGroup, Model model, RedirectAttributes redirectAttributes) {
		for(EleSourceImage esi:eleSourceImageGroup.getEleSourceImageList()){
			esi.setGroup(eleSourceImageGroup);
		}
		if (!beanValidator(model, eleSourceImageGroup)){
			return form(eleSourceImageGroup, model);
		}
		eleSourceImageGroupService.save(eleSourceImageGroup);
		addMessage(redirectAttributes, "保存图片资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/image/eleSourceImageGroup/?repage";
	}
	
	@RequiresPermissions("source:image:eleSourceImageGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceImageGroup eleSourceImageGroup, RedirectAttributes redirectAttributes) {
		eleSourceImageGroupService.delete(eleSourceImageGroup);
		addMessage(redirectAttributes, "删除图片资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/image/eleSourceImageGroup/?repage";
	}

	@RequiresPermissions("source:image:eleSourceImageGroup:check")
	@RequestMapping(value = "check")
	public String check(EleSourceImageGroup eleSourceImageGroup, RedirectAttributes redirectAttributes) {
		eleSourceImageGroupService.delete(eleSourceImageGroup);
		addMessage(redirectAttributes, "审核图片资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/image/eleSourceImageGroup/?repage";
	}

}